package com.bacon.gmall.product.controller;

import com.bacon.gmall.common.result.Result;
import com.bacon.gmall.model.product.*;
import com.bacon.gmall.product.service.BaseManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author： bacon、
 * @version： 1.0
 * @create： 2020-12-19 13:07
 * @description:
 */
@Api(tags = "商品基础属性接口")
@RestController
@RequestMapping("/admin/product")
public class BaseManageController {

    @Autowired
    private BaseManageService baseManageService;


    /**
     * 商品上架
     */
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId) {
        baseManageService.onSale(skuId);
        return Result.ok();
    }

    /**
     * 商品下架
     */
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId) {
        baseManageService.cancelSale(skuId);
        return Result.ok();
    }

    /**
     * 查询SKU分页查询列表
     *
     * @return
     */
    @GetMapping("/list/{page}/{limit}")
    public Result list(@PathVariable Long page,
                       @PathVariable Long limit) {
        IPage<SkuInfo> iPage = baseManageService.list(page, limit);
        return Result.ok(iPage);
    }

    /**
     * 保存SKU
     * 需要添加4表
     */
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo) {
        baseManageService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    /**
     * 添加SPU
     */
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo) {
        baseManageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    /**
     * 根据商品id查询所有图片
     */
    @GetMapping("/spuImageList/{spuId}")
    public Result spuImageList(@PathVariable Long spuId) {
        List<SpuImage> spuImages = baseManageService.spuImageList(spuId);
        return Result.ok(spuImages);
    }

    /**
     * 根据商品ID查询 销售属性及属性值集合
     */
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable Long spuId) {
        List<SpuSaleAttr> saleAttrList = baseManageService.spuSaleAttrList(spuId);
        return Result.ok(saleAttrList);
    }

    /**
     * 三级分类的属性
     *
     * @param baseAttrInfo
     * @return
     */
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo) {
        baseManageService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }

    //根据1、2、3级分类id，查询平台属性级属性值集合
    @GetMapping("attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(
            @PathVariable Long category1Id,
            @PathVariable Long category2Id,
            @PathVariable Long category3Id) {
        List<BaseAttrInfo> baseAttrInfoList = baseManageService.attrInfoList(
                category1Id,
                category2Id,
                category3Id);

        return Result.ok(baseAttrInfoList);
    }

    @ApiOperation("查询1级平台属性")
    @GetMapping("getCategory1")
    public Result getCategory1() {
        List<BaseCategory1> list = baseManageService.getCategory1();
        return Result.ok(list);
    }

    @ApiOperation("根据1级平台属性查询2级平台属性")
    @GetMapping("getCategory2/{category1}")
    public Result getCategory2(@PathVariable Long category1) {
        List<BaseCategory2> list = baseManageService.getCategory2(category1);
        return Result.ok(list);
    }

    @ApiOperation("根据2级平台属性查询3级平台属性")
    @GetMapping("getCategory3/{category2}")
    public Result getCategory3(@PathVariable Long category2) {
        List<BaseCategory3> list = baseManageService.getCategory3(category2);
        return Result.ok(list);
    }

    /**
     * 品牌页面查询并且进行分页
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("baseTrademark/{page}/{limit}")
    public Result baseTrademark(
            @PathVariable Long page,
            @PathVariable Long limit) {
        IPage<BaseTrademark> Page = baseManageService.baseTrademark(page, limit);
        return Result.ok(Page);
    }

    /**
     * 根据id获取品牌信息
     *
     * @param id
     * @return
     */
    @GetMapping("/baseTrademark/get/{id}")
    public Result getBaseTrademark(@PathVariable Long id) {
        BaseTrademark baseTrademark = baseManageService.getBaseTrademark(id);
        return Result.ok(baseTrademark);
    }

    /**
     * 回显点击修改按钮的平台属性
     * getAttrValueList/' + attrId,
     *
     * @param attrId
     * @return
     */
    @GetMapping("/getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable Long attrId) {
        List<BaseAttrValue> attrInfo = baseManageService.getAttrValueList(attrId);
        return Result.ok(attrInfo);
    }

    /**
     * 修改品牌图片
     * 请求路径：baseTrademark/update
     */
    @PutMapping("/baseTrademark/update")
    public Result updateBaseTrademark(@RequestBody BaseTrademark baseTrademark) {
        baseManageService.updateBaseTrademark(baseTrademark);
        return Result.ok();
    }


    /**
     * 根据category3Id进行分页
     *
     * @param page
     * @param limit
     * @param category3Id 三级分类id
     * @return
     */
    @GetMapping("/{page}/{limit}")
    public Result spuList(
            @PathVariable Integer page,
            @PathVariable Long limit,
            Long category3Id) {
        IPage<SpuInfo> iPage = baseManageService.spuList(page, limit, category3Id);
        return Result.ok(iPage);
    }


    /**
     * 获取所有的品牌
     * baseTrademark/getTrademarkList
     */
    @GetMapping("baseTrademark/getTrademarkList")
    public Result getTrademarkList() {
        List<BaseTrademark> list = baseManageService.getTrademarkList();
        return Result.ok(list);
    }

    /**
     * 获取字典表商品基本属性
     * baseSaleAttrList
     */
    @GetMapping("/baseSaleAttrList")
    public Result getSaleAttrList() {
        List<BaseSaleAttr> list = baseManageService.getSaleAttrList();
        return Result.ok(list);
    }

    /**
     * 新建品牌
     *
     * @param baseTrademark
     * @return
     */
    @PostMapping("baseTrademark/save")
    public Result saveBaseTrademark(@RequestBody BaseTrademark baseTrademark) {
        baseManageService.saveBaseTrademark(baseTrademark);
        return Result.ok();
    }

    /**
     * 删除品牌
     * baseTrademark/remove/12313
     */
    @DeleteMapping("baseTrademark/remove/{trademarkId}")
    public Result removeBaseTrademark(@PathVariable Long trademarkId) {
        baseManageService.removeBaseTrademark(trademarkId);
        return Result.ok();
    }

}
